﻿//+-------------------------------------------------------------------------------------------------------------------------------------------------------
//
//Description: GuestAgentStatus
//Copyright © Microsoft Corporation
//Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
//You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
//
//THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
//INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. 
//See the Apache Version 2.0 License for specific language governing permissions and limitations under the License.
// 
//--------------------------------------------------------------------------------------------------------------------------------------------------------
using System;
using System.IO;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace VmroleAuthor.ViewModels
{
    public enum GuestAgentStatusEnum
    {
        Ready,
        NotReady,
    }

    public enum HandlerAggregateStatus
    {
        Ready,
        NotReady,
        Installing,
        Unresponsive,
    }

    public enum HandlerStatusStates
    {
        Transitioning,
        Error,
        Success,
        Warning,
    }

    [DataContract]
    public class GuestAgentStatus : IExtensibleDataObject
    {
        [DataMember(EmitDefaultValue = false, Order = 1)]
        public string ProtocolVersion { get; set; }

        [DataMember(EmitDefaultValue = false, Order = 2, Name = "Timestamp")]
        private string IsoTimestampString { get; set; }

        public DateTime Timestamp
        {
            get
            {
                return (string.IsNullOrEmpty(this.IsoTimestampString) ? DateTime.MinValue : DateTime.Parse(this.IsoTimestampString));
            }
            set
            {
                if (value.Equals(DateTime.MinValue))
                {
                    this.IsoTimestampString = null;
                }
                else
                {
                    this.IsoTimestampString = value.ToUniversalTime().ToString("s");
                }
            }
        }

        [DataMember(EmitDefaultValue = false, Order = 3)]
        public string GuestAgentVersion { get; set; }

        [DataMember(EmitDefaultValue = false, Name = "Status", Order = 4, IsRequired = true)]
        private string _guestAgentStatus;

        public GuestAgentStatusEnum Status
        {
            get
            {
                GuestAgentStatusEnum retVal;
                if (Enum.TryParse(_guestAgentStatus, true, out retVal))
                {
                    return retVal;
                }
                return GuestAgentStatusEnum.NotReady;
            }
            set
            {
                _guestAgentStatus = value.ToString();
            }
        }

        [DataMember(EmitDefaultValue = false, Order = 5)]
        public int Code { get; set; }

        [DataMember(EmitDefaultValue = false, Order = 6)]
        public GuestAgentMessage Message { get; set; }

        [DataMember(EmitDefaultValue = false, Order = 7)]
        public GuestAgentFormattedMessage FormattedMessage { get; set; }

        public ExtensionDataObject ExtensionData { get; set; }

        public static GuestAgentStatus ParseObjectFromXml(XNode xNode, XNamespace xNamespace)
        {
            XmlSerializer xSerial = new XmlSerializer(typeof(GuestAgentStatus), xNamespace.NamespaceName);
            GuestAgentStatus gas;

            try
            {
                gas = (GuestAgentStatus)xSerial.Deserialize(new StringReader(xNode.ToString()));
            }
            catch (InvalidOperationException)
            {
                gas = null;
            }

            return gas;
        }
    }

    [DataContract]
    public class GuestAgentMessage : IExtensibleDataObject
    {
        [DataMember(EmitDefaultValue = false, Order = 1)]
        public string MessageResourceId { get; set; }

        [DataMember(EmitDefaultValue = false, Order = 2)]
        public GuestAgentMessageParamList ParamList { get; set; }

        public ExtensionDataObject ExtensionData { get; set; }
    }

    [CollectionDataContract(ItemName = "Param")]
    public class GuestAgentMessageParamList : List<string>
    {
        public GuestAgentMessageParamList()
        {
        }

        public GuestAgentMessageParamList(IEnumerable<string> list)
            : base(list)
        {
        }
    }

    [DataContract]
    public class GuestAgentFormattedMessage : IExtensibleDataObject
    {
        // Language is either "Language" or "Language-Locale".
        [DataMember(EmitDefaultValue = false, Order = 1)]
        public string Language { get; set; }

        [DataMember(EmitDefaultValue = false, Order = 2)]
        public string Message { get; set; }

        public ExtensionDataObject ExtensionData { get; set; }
    }

    [CollectionDataContract(ItemName = "ResourceExtensionStatus")]
    public class ResourceExtensionStatusList : List<ResourceExtensionStatus>
    {
        public ResourceExtensionStatusList()
        {
        }

        public ResourceExtensionStatusList(IEnumerable<ResourceExtensionStatus> list)
            : base(list)
        {
        }
    }

    [DataContract]
    public class ResourceExtensionStatus : IExtensibleDataObject
    {
        [DataMember(EmitDefaultValue = false, Order = 1)]
        public string HandlerName { get; set; }

        [DataMember(EmitDefaultValue = false, Order = 2)]
        public string Version { get; set; }

        [DataMember(EmitDefaultValue = false, Name = "Status", Order = 3)]
        private string _handlerAggregateStatus;

        public HandlerAggregateStatus Status
        {
            get
            {
                HandlerAggregateStatus retVal;
                if (Enum.TryParse(_handlerAggregateStatus, true, out retVal))
                {
                    return retVal;
                }
                return HandlerAggregateStatus.NotReady;
            }
            set
            {
                _handlerAggregateStatus = value.ToString();
            }
        }

        [DataMember(EmitDefaultValue = false, Order = 4)]
        public int Code { get; set; }

        [DataMember(EmitDefaultValue = false, Order = 5)]
        public GuestAgentMessage Message { get; set; }

        [DataMember(EmitDefaultValue = false, Order = 6)]
        public GuestAgentFormattedMessage FormattedMessage { get; set; }

        [DataMember(EmitDefaultValue = false, Order = 7)]
        public ResourceExtensionConfigurationStatus ExtensionSettingStatus { get; set; }

        public ExtensionDataObject ExtensionData { get; set; }

        public static ResourceExtensionStatus ParseObjectFromXml(XNode xNode, XNamespace xNamespace)
        {
            XmlSerializer xSerial = new XmlSerializer(typeof(ResourceExtensionStatus), xNamespace.NamespaceName);
            ResourceExtensionStatus res;

            try
            {
                res = (ResourceExtensionStatus)xSerial.Deserialize(new StringReader(xNode.ToString()));
            }
            catch (InvalidOperationException ioe)
            {
                Console.Write(ioe.Message);
                res = null;
            }

            return res;
        }
    }

    [DataContract]
    public class ResourceExtensionConfigurationStatus : IExtensibleDataObject
    {
        [DataMember(EmitDefaultValue = false, Order = 1, Name = "Timestamp")]
        private string IsoTimestampString { get; set; }

        public DateTime Timestamp
        {
            get
            {
                return (string.IsNullOrEmpty(this.IsoTimestampString) ? DateTime.MinValue : DateTime.Parse(this.IsoTimestampString));
            }
            set
            {
                if (value.Equals(DateTime.MinValue))
                {
                    this.IsoTimestampString = null;
                }
                else
                {
                    this.IsoTimestampString = value.ToUniversalTime().ToString("s");
                }
            }
        }

        [DataMember(EmitDefaultValue = false, Order = 2, Name = "ConfigurationAppliedTime")]
        private string IsoConfigurationAppliedTimeString { get; set; }

        public DateTime ConfigurationAppliedTime
        {
            get
            {
                return (string.IsNullOrEmpty(this.IsoConfigurationAppliedTimeString) ? DateTime.MinValue : DateTime.Parse(this.IsoConfigurationAppliedTimeString));
            }
            set
            {
                if (value.Equals(DateTime.MinValue))
                {
                    this.IsoConfigurationAppliedTimeString = null;
                }
                else
                {
                    this.IsoConfigurationAppliedTimeString = value.ToUniversalTime().ToString("s");
                }
            }
        }

        [DataMember(EmitDefaultValue = false, Order = 3)]
        public string Name { get; set; }

        [DataMember(EmitDefaultValue = false, Order = 4)]
        public string Operation { get; set; }

        [DataMember(EmitDefaultValue = false, Name = "Status", Order = 5)]
        private string _status;

        public HandlerStatusStates Status
        {
            get
            {
                HandlerStatusStates retVal;
                if (Enum.TryParse(_status, true, out retVal))
                {
                    return retVal;
                }
                return HandlerStatusStates.Error;
            }
            set
            {
                _status = value.ToString();
            }
        }

        [DataMember(EmitDefaultValue = false, Order = 6)]
        public int Code { get; set; }

        [DataMember(EmitDefaultValue = false, Order = 7)]
        public GuestAgentMessage Message { get; set; }

        [DataMember(EmitDefaultValue = false, Order = 8)]
        public GuestAgentFormattedMessage FormattedMessage { get; set; }

        [DataMember(EmitDefaultValue = false, Order = 9)]
        public SubStatusList SubStatusList { get; set; }

        public ExtensionDataObject ExtensionData { get; set; }
    }

    [CollectionDataContract(Name="SubStatusList", ItemName = "SubStatus")]
    public class SubStatusList : List<SubStatus>
    {
        public SubStatusList()
        {
        }

        public SubStatusList(IEnumerable<SubStatus> list)
            : base(list)
        {
        }
    }

    [DataContract]
    public class SubStatus : IExtensibleDataObject
    {
        [DataMember(EmitDefaultValue = false, Order = 1)]
        public string Name { get; set; }

        [DataMember(EmitDefaultValue = false, Name = "Status", Order = 2)]
        private string _status;

        public HandlerStatusStates Status
        {
            get
            {
                HandlerStatusStates retVal;
                if (Enum.TryParse(_status, true, out retVal))
                {
                    return retVal;
                }
                return HandlerStatusStates.Error;
            }
            set
            {
                _status = value.ToString();
            }
        }

        [DataMember(EmitDefaultValue = false, Order = 3)]
        public int Code { get; set; }

        [DataMember(EmitDefaultValue = false, Order = 4)]
        public GuestAgentMessage Message { get; set; }

        [DataMember(EmitDefaultValue = false, Order = 5)]
        public GuestAgentFormattedMessage FormattedMessage { get; set; }

        public ExtensionDataObject ExtensionData { get; set; }
    }
}